Exploring Terraform Outputs

Explore the output variables and check on the cluster we created.

Viewing the output file#

We’ll retrieve the nodes of the newly created Kubernetes cluster and see what we’ve got. But, before we do that, we need to create a kubeconfig file that will provide kubectl the information on how to access the cluster. We could do that right away with az CLI, but we’ll make it a bit more complicated.

The structure of a cluster that we'll be viewing later

To create kubeconfig, we need to know the name of the cluster and the resource group in which it’s running. We might have that information in our heads, but let’s imagine that’s not the case. What if we forgot it, or didn’t pay attention before? That gives us a perfect opportunity to introduce yet another Terraform feature.

We can define outputs with the information we need, as long as that information is available in Terraform state. Here is the content of the output.tf file.

Viewing the output.tf file

We’re specifying which data should be output by Terraform. Such outputs are generated at the end of the terraform apply process, and we’ll see that later.

For now, we’re only interested in the outputs so that we can use them to deduce the name of the cluster and the resource group and retrieve the credentials for kubeconfig.

If we want to see all the outputs, we can simply refresh. That would update the state file with the information about the physical resources Terraform is tracking and, more importantly, showing us those outputs. We’ll use the commands that follow.

Command to execute terraform refresh

The output, limited to the relevant parts, is as follows.

Output of refresh command

We can clearly see the name of the cluster, the region, and the resource group, but that’s not what we really need. We’re not interested in seeing that information, but rather in using it to construct the command that will retrieve the credentials. We can accomplish that with the terraform output command.

Viewing the cluster name

The output is as follows.

Output of above Command

Checking the cluster#

Now we know how to retrieve the output of a single value, so let’s use that to construct the command that will retrieve the credentials.

Commands to retrieve credentials

We specified that kubeconfig should be in the current directory by exporting the environment variable KUBECONFIG. Further on, we retrieved the credentials using az. What matters, apart from the obvious need to retrieve the credentials, is that we used terraform output to retrieve the data we need and pass them to az.

Now we should be able to check the cluster that Terraform created for us. We’ll retrieve the nodes using the following command.

The cluster is empty for now
Output of kubectl get nodes

We can see that there are three worker nodes in the cluster. That number coincides with the minimum number of nodes we specified for the default worker node pool. Our cluster is now ready for use. Nevertheless, we should explore how to create additional worker node pools.

Please provide values for the following:
AZURE_BUCKET_NAME
Not Specified...
/
main.sh
backend.tf
k8s-control-plane.tf
output.tf
provider.tf
storage.tf
variables.tf
terraform.tfstate
Code playground

Creating the Control Plane

Creating Worker Nodes